import 'package:flutter_admin/api/odoo/dynamic_data_api.dart';
import 'package:flutter_admin/api/odoo/finished_in_scan_odoo_api.dart';
import 'package:flutter_admin/api/odoo/stock_move_line_odoo_api.dart';
import 'package:flutter_admin/api/odoo/stock_quant_odoo_api.dart';
import 'package:flutter_admin/generated/l10n.dart';
import 'package:flutter_admin/models/odoo/finish_in.dart';
import 'package:flutter_admin/models/odoo/stock_move_line.dart';
import 'package:flutter_admin/models/odoo/stock_quant.dart';
import 'package:flutter_admin/pages/common/cry/cry_button.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input.dart';
import 'package:flutter/material.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_input_num.dart';
import 'package:flutter_admin/pages/common/cry/form/cry_select.dart';
import 'package:flutter_admin/pages/common/cry/model/response_body_api.dart';
import 'package:flutter_admin/pages/common/cry/utils/cry_utils.dart';
import 'package:flutter_admin/pages/common/cry/utils/http_util.dart';
import 'package:flutter_admin/pages/common/cry/vo/select_option_vo.dart';
import 'package:flutter_admin/utils/eventbus.dart';
import 'package:flutter_admin/utils/store_util.dart';
import 'package:qrscan/qrscan.dart' as scanner;
import 'package:quiver/strings.dart';

class HWWeigh extends StatefulWidget {
  HWWeigh({Key? key,required this.stockQuant}) : super(key: key);

  StockQuant stockQuant;

  @override
  _HWWeighState createState() => _HWWeighState();
}

class _HWWeighState extends State<HWWeigh> {
  final GlobalKey<FormState> formKey = GlobalKey<FormState>();
  final GlobalKey<ScaffoldState> scaffoldStateKey = GlobalKey<ScaffoldState>();
  StockQuant stockQuant = StockQuant();
  bool isLoading = false;
  String passWord = '';
  FocusNode _focusNode = FocusNode();
  @override
  void initState() {
    stockQuant = widget.stockQuant;
    super.initState();
    _loadSelectVO();
  }

  @override
  void dispose() {
    StoreUtil.write("key", "HWCheckDetail");
    super.dispose();
  }

  _loadSelectVO() async {
    var res = await DynamicDataApi.getModels(model: "huawei.lock_pwd");
    if( res.success! ){
      passWord = res.data[0]["pwd"];
    }
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var form = Form(
      key: formKey,
      child: Wrap(
        children: <Widget>[
          CryInput(
            label: '产品名称',
            enable: false,
            value: stockQuant.productName,
          ),
          CryInput(
            label: '偏差值',
            enable: false,
            value: stockQuant.weighFloat,
          ),
          CryInput(
            label: '重量预设值',
            enable: false,
            value: stockQuant.weighDefault,
          ),
          CryInputNum(
            label: '重量',
            value: stockQuant.weigh,
            onSaved: (v){
              stockQuant.weigh = v;
            },
            validator: (v) {
              return v!.isEmpty ? "请输入重量" :num.parse(v) ==0? "重量不可以为0": null;
            },
          ),
        ],
      ),
    );

    var footerBar = ButtonBar(
      alignment: MainAxisAlignment.center,
      children: <Widget>[
        CryButton(
          label: "确定",
          onPressed: () {
            FormState form = formKey.currentState!;
            if (!form.validate()) {
              return;
            }
            form.save();
            if(isBlank(stockQuant.weighDefault) || isBlank(stockQuant.weighFloat)){
              CryUtils.message("缺少对应的重量属性值。请联系管理员！");
              return ;
            }
            num start = num.parse(stockQuant.weighDefault!) - num.parse(stockQuant.weighDefault!)*num.parse(stockQuant.weighFloat!)/100;
            num end = num.parse(stockQuant.weighDefault!) + num.parse(stockQuant.weighDefault!)*num.parse(stockQuant.weighFloat!)/100;
            if(stockQuant.weigh!>= start && stockQuant.weigh! <= end){
              StockQuantOdooApi.saveOrUpdate(
                      {'id':int.parse(stockQuant.id!),
                        'is_weigh': '1',
                        'weigh': stockQuant.weigh
                      }).then((ResponseBodyApi res) {
                if (!res.success!) {
                  return;
                }
                Navigator.pop(context, true);
                CryUtils.message(S.of(context).saved);

              });
            }else{
              isLoading = true;
              _focusNode.requestFocus();
              setState(() {});
              return ;
            }
          },
          iconData: Icons.save,
        ),
        CryButton(
          label: S.of(context).cancel,
          onPressed: () {
            Navigator.pop(context);
          },
          iconData: Icons.cancel,
        )
      ],
    );
    var result = Scaffold(
      key: scaffoldStateKey,
      appBar: AppBar(
        title: Text("称重确认"),
      ),
      body: SingleChildScrollView(
        child: Column(
          children: [form],
        ),
      ),
      bottomNavigationBar: footerBar,
    );
    return Stack(
      alignment: Alignment.center,
      fit: StackFit.expand, //未定位widget占满Stack整个空间
      children: <Widget>[
        // Positioned(
        //   left: 18.0,
        //   child: Text("I am Jack"),
        // ),
        SizedBox(
          width: 650,
          height: 650,
          child: result,
        ),
        isLoading
            ? Positioned(
          // top: 18.0,
          child: Container(
            // width: 800,
            // height: 800,
            color: Colors.black87,
            child: Center(
              child: Container(
                  width: 200,
                  color: Colors.white,
                  child: TextFormField(
                    focusNode: _focusNode,
                    style: TextStyle(color: Colors.black),
                    decoration: InputDecoration(
                      labelText: '密码',
                      contentPadding: EdgeInsets.symmetric(horizontal: 10),
                    ),
                    // controller: TextEditingController(text: passWord),
                    onChanged: (v) {
                      if(passWord == v){
                        isLoading = false;
                        setState(() {});
                      }
                    },
                  ),)
              // child: Container(
              //   color: Colors.white,
              //   child: CryInput(
              //   label: '密码',
              //   width: 200,
              //   onChange: (v) {
              //     if(passWord == v){
              //       isLoading = false;
              //       // scan1 = '';
              //       // scan2 = '';
              //       setState(() {});
              //     }
              //   },
              // ),),
            ),
          ),
        )
            : Container(),
      ],
    );
  }

}
